home *** CD-ROM | disk | FTP | other *** search
/ ADA Programming Guide / ADA Programming Guide.iso / ada_pcdp / ada / phila.ada < prev    next >
Text File  |  1996-01-30  |  1KB  |  39 lines

  1. with Text_IO; use Text_IO;
  2. with Semaphore_Package; use Semaphore_Package;
  3. procedure Phila is
  4.  
  5.   type Node_ID is range 0..4;
  6.  
  7.   Fork: array(Node_ID) of Binary_Semaphore := (others => Init(1));
  8.  
  9.   task type Nodes is
  10.     entry Init(ID: Node_ID);
  11.   end Nodes;
  12.  
  13.   Node: array(Node_ID) of Nodes;
  14.  
  15.   task body Nodes is
  16.     I:           Node_ID;
  17.   begin
  18.     accept Init(ID: Node_ID) do
  19.       I := ID;
  20.     end Init;
  21.     for M in 1..5 loop
  22.       Put_Line(" " & Node_ID'Image(I) & " thinking");
  23.       if I = 4 then Wait(Fork(0)); else Wait(Fork(I)); end if;
  24.       Put_Line(" " & Node_ID'Image(I) & " first fork taken");
  25.       if I = 4 then Wait(Fork(4)); else Wait(Fork((I+1) mod 5)); end if;
  26.       Put_Line(" " & Node_ID'Image(I) & " critical section");
  27.       Signal(Fork(I));
  28.       Put_Line(" " & Node_ID'Image(I) & " first fork released");
  29.       Signal(Fork((I+1) mod 5));
  30.       Put_Line(" " & Node_ID'Image(I) & " second fork released");
  31.     end loop;
  32.   end Nodes;
  33.  
  34. begin
  35.   for J in Node_ID loop
  36.     Node(J).Init(J);
  37.   end loop;
  38. end Phila;
  39.